package com.moonshine.controller;

import com.moonshine.pojo.Dept;
import com.moonshine.pojo.Result;
import com.moonshine.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;


//    @RequestMapping(value = "/depts", method = RequestMethod.GET) // 指定接口请求为 get

    /**
     * 查询部门列表
     * @return
     */
    @GetMapping
    public Result getDeptList(){
        log.info("查询全部数据");
        List<Dept> deptList = deptService.getDeptList();

        return Result.success(deptList);
    }

    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public Result addDept(@RequestBody  Dept dept){
        log.info("新增部门数据: {}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门数据：{}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable Integer id){
        log.info("根据id删除部门：{}", id);
        deptService.deleteDept(id);
        return Result.success();
    }
}
